/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dd2.bl.PerfilBlLocal;
import mx.its.ds2.modelo.Perfil;

/**
 *
 * @author Piter
 */
@ManagedBean
@SessionScoped
public class PerfilBean {
    @EJB
    private PerfilBlLocal perfilBl;
    
    private List<Perfil> lista;
    private Perfil profile;
    private String accion;
    

    /**
     * Creates a new instance of PerfilBean
     */
    public PerfilBean() {
        profile = new Perfil();
            
        }
    public List<Perfil> getLista(){
        lista=perfilBl.getAll();
        return lista;
    }

    public Perfil getProfile() {
        return profile;
    }

    public void setProfile(Perfil profile) {
        this.profile = profile;
    }
     public String procesarAccion() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            perfilBl.Register(profile);
        } else if (accion.equalsIgnoreCase("Editar")) {
            perfilBl.modify(profile);
        }else if (accion.equalsIgnoreCase("Eliminar")){
             perfilBl.delete(profile);
        }

        accion = "";
        return "Perfil";
    }
     public void prepararNuevo(ActionEvent evt){
        profile = new Perfil();
        accion="nuevo";
        
    }
     public void prepararEditar(ActionEvent evt) {
//        System.out.println(evt.getComponent().getClientId());
//        String arr[]= evt.getComponent().getClientId().split(":");
//        String id=arr[2];
//        int index=Integer.parseInt(id);
//        
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        profile = lista.get(index);
        accion="editar";
    }
     public void eliminar(ActionEvent evt){
        accion="eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        profile = lista.get(index);
        
    }
      public String marrirris() {
        String h = "quiero ir a chapala";
        if (accion.equalsIgnoreCase("Nuevo")) {
            h = "Perfil";
        } else if (accion.equalsIgnoreCase("Editar")) {
            h = "PerfilCrearEditar";
        } else if (accion.equalsIgnoreCase("Eliminar")) {
            h = "Perfil";
        }

        accion = "";
        return h;
    }
     
    
    
}
